﻿using System;
using System.Collections.Generic;

namespace MiniAspNetCore
{
    /// <summary>
    /// web宿主的构建者
    /// </summary>
    public class WebHostBuilder : IWebHostBuider
    {
        private IServer _server;
        private readonly IList<Action<IApplicationBuilder>> _configs = new List<Action<IApplicationBuilder>>();

        /// <summary>
        /// 构建web宿主
        /// </summary>
        /// <returns></returns>
        public IWebHost Build()
        {
            var app = new ApplicationBuilder();

            //配置管道
            foreach (var cnf in _configs)
            {
                cnf(app);
            }

            return new WebHost(_server, app.Build());
        }

        /// <summary>
        /// 配置管道
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public IWebHostBuider Config(Action<IApplicationBuilder> config)
        {
            _configs.Add(config);
            return this;
        }

        /// <summary>
        /// 使用哪个服务器
        /// </summary>
        /// <param name="server"></param>
        /// <returns></returns>
        public IWebHostBuider UseServer(IServer server)
        {
            _server = server;
            return this;
        }
    }
}
